/*-----------------------------------------------------------------------------------
Name: Sangyoon Park
Date: January 24 2023
This do file does : 
	Create tables for online appendix
-----------------------------------------------------------------------------------*/
clear all
set matsize 10000
set maxvar 10000
set more off
capture log close


#delim ;


/*-----------------------------------------------------------------------------------
			 Set up
-----------------------------------------------------------------------------------*/
*** Setting up files;
	glo datafile "qu_analysis"; 	// type in the name of the datafile that you want to create;

*** Set up program;
 ** Number Format;
	capture program drop autoformat;
	program define autoformat;

	if int(`1')==`1' global format "%12.0f";
	else if abs(`1')>10 global format "%9.1f";
	else if abs(`1')>1 global format "%9.2f";
	else if abs(`1')>.1 global format "%9.3f";
	else if abs(`1')>.01 global format "%9.3f";
	else if abs(`1')>.001 global format "%9.3f";
	else if abs(`1')>.0001 global format "%9.4f";
	else if abs(`1')<.00001 global format "%9.5f";

	end;
	
 ** Tex Table format;
  	capture program drop head_foot;
	program define head_foot;

	syntax, [caption(str)] [label(str)] [notes(str)] [size(str)] [columns(str)] [sideways] [long];

	local estimates=r(names);
	if "`columns'"=="" local columns:word count `estimates';
	if "`sideways'"=="sideways" local table sidewaystable;
	if "`long'"=="long" local table longtable;
	else local table table;
	local col = `columns'+1;

	global header \begin{`table'}[H] \footnotesize \begin{center};
		if "`size'"!="" global header $header \begin{`size'};
		if "`caption'"!="" global header $header \caption{`caption'\label{table:`label'}} ;
		global header $header  ///;
		\begin{adjustbox}{max width=\textwidth} ///;
		\begin{threeparttable} ///;
		\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi} ;
		global header $header ///;
		\begin{tabular}{l*{`columns'}{c}}\toprule[1.5pt];

	global header_mid \tabularnewline;

	global panel_title \multicolumn{`col'}{l}{Panel}\tabularnewline \midrule;

	global footer_mid \midrule ;
	global footer \bottomrule[1.2pt] ///
		\end{tabular} ;
		if "`notes'"!="" {;
			global footer $footer \begin{tablenotes}[flushleft] ///;
			`notes' ///;
			\end{tablenotes} ;
			};
		global footer $footer \end{threeparttable}\end{adjustbox};
		if "`size'"!="" global footer $footer \end{`size'};
		global footer $footer \end{center} \end{`table'} 	;
	end;

*** table setting;
	local stat_balance cells("mean(fmt(2)) b(fmt(3) star)" "sd(par fmt(2)) t(par fmt(2))") ///;
			label noobs collabels(none) nonote ;
	local stat_table cells("mean(fmt(2))" "sd(par fmt(2))") label collabels(none) nonote ;

	local reg_table_format cells(b(fmt(3) star) se(par fmt(3))) star(* 0.10 ** 0.05 *** 0.01) ///;
		stats(N r2 ymean, fmt(0 3 2) labels("N" "R-Squared" "Mean of Dep. Var."))  ///;
		label mlabels(none) collabels(none) nonotes;

	local reg_table_format2 cells(b(fmt(3) star) se(par fmt(3))) star(* 0.10 ** 0.05 *** 0.01) ///;
		stats(ymean, fmt(3) labels("Mean of Dep. Var."))  ///;
		label mlabels(none) collabels(none) nonotes;

		
/*-----------------------------------------------------------------------------------
			 		DATA = GAME
-----------------------------------------------------------------------------------*/		

	import excel "${raw_path}/DATA_GAME_ALL.xlsx", sheet("Sheet1") firstrow ;
	tempfile game;
	save `game';
	clear;		
	
/*-----------------------------------------------------------------------------------
			 		DATA = EXPORTER SURVEY
-----------------------------------------------------------------------------------*/		

	use "${data_path}/INTERMEDIARY_LONG.dta", clear;
	rename age firmage;
		egen group_id = group(strata st_tgroup);
		gen tr_farm = (st_tgroup == 2 | st_tgroup == 6);
		gen tr_int = (st_tgroup == 3 | st_tgroup == 7);
		gen tr_joint = (st_tgroup == 4 | st_tgroup == 8);
		gen certelig = (st_tgroup == 5 | st_tgroup == 6 | st_tgroup == 7 | st_tgroup == 8);
		gen tr_fXcert = tr_farm * certelig;
		gen tr_iXcert = tr_int * certelig;
		gen tr_jXcert = tr_joint * certelig;	
	tempfile intdata;
	save `intdata';
	clear;	
	
/*-----------------------------------------------------------------------------------
			 		DATA = FARMER SURVEY
-----------------------------------------------------------------------------------*/			
		
	
*** Read data;
	
	use "${data_path}/FARMER_LONG_SETUP", replace;
	
*** Drop control communes;

	drop if control_commune == 1;	
	
*** Generate dummy for treatment groups;	
	
	tab st_tgroup, gen(dum_tgroup);

*** Cluster for standard errors;

	egen group_id = group(strata st_tgroup);

*** Generate treatment levels and interaction terms;

	gen tr_farm = (st_tgroup == 2 | st_tgroup == 6);
	gen tr_int = (st_tgroup == 3 | st_tgroup == 7);
	gen tr_joint = (st_tgroup == 4 | st_tgroup == 8);
	gen certelig = (st_tgroup == 5 | st_tgroup == 6 | st_tgroup == 7 | st_tgroup == 8);
	gen tr_fXcert = tr_farm * certelig;
	gen tr_iXcert = tr_int * certelig;
	gen tr_jXcert = tr_joint * certelig;
	gen control = (st_tgroup == 1 | st_tgroup == 5);
	

*** Drop large farms (farm size larger than five hectares);

	drop if ha_whitedragon >= 5;
	
*** Define local variables;
	
	local dummy_coef dum_tgroup2 dum_tgroup3 dum_tgroup4 dum_tgroup5 dum_tgroup6 dum_tgroup7 dum_tgroup8;
	local treat_coef tr_farm tr_int tr_joint certelig tr_fXcert tr_iXcert tr_jXcert;


	local balance age female edu_second exp_dragon ha_whitedragon white_age_less1 white_age_less3 white_age_less10 white_age_less20 white_age_more20 anycert_base loan_any_base save_bank_base mean_trust_base mean_business_base mean_confidence_base raven_total present_bias_base gap_comply_base hours_base totalvolume_base price_base log_cost_fert_base log_cost_pest_base log_cost_facequip_base log_cost_labor_base log_cost_utility_base;
	local intbalance int_age int_size_facility int_volume_china_fu0 int_volume_asia_fu0 int_volume_eu_fu0 int_volume_dom_fu0 int_dum_firmtype1 int_dum_firmtype2 int_dum_firmtype3;
	
/*-----------------------------------------------------------------------------------
			 		Label 
-----------------------------------------------------------------------------------*/
	lab var tr_farm "Farmer Training";
	lab var tr_int "Exporter Training";
	lab var tr_joint "Joint Training";
	lab var certelig "Certificate Eligibility (C.E.)";
	lab var tr_fXcert "C.E. $\times$ Farmer Training";
	lab var tr_iXcert "C.E. $\times$ Exporter Training";
	lab var tr_jXcert "C.E $\times$ Joint Training";
	lab var std_knowledge "Farmer GAP Knowledge";
	lab var std_perception "Farmer Food Safety Awareness";
	lab var int_std_knowledge "Exporter GAP Knowledge";
	lab var dist_nearest_treated "Distance to nearest jointly trained farmer (km)";
	lab var farmer_in2 "Number of farmers within 2km distance";
	lab var trader_in3 "Number of exporters within 3km distance";
	lab var trader_in2 "Number of exporters within 2km distance";
	lab var dist_jointtrader "Distance to nearest jointly trained exporter (km)";

	
tempfile maindata;
save `maindata';
clear;
	
/*-----------------------------------------------------------------------------------


			 		Tables for Online Appendix

					
-----------------------------------------------------------------------------------*/	

/*-----------------------------------------------------------------------------------
			OUTCOME = GAP Audit by Management Area (Online Appendix Table A-4)
-----------------------------------------------------------------------------------*/

use `maindata';

** Setting;
	loc titles "& Total & Pesticide & Equipment & Hygiene & Soil  & Fertilizer \\ ";
	loc numbers "& (1) & (2) & (3) & (4) & (5) & (6) \\ \midrule";
	loc reg_table cells(b(fmt(3) star) se(par fmt(3))) starlevels(* 0.10 ** 0.05 *** 0.01) ///
		stats(betafi betafj betaij betafij ymean_control ysd_control r2 N, fmt(2 2 2 2 2 2 2 0) ///
		labels("P-value ($ \text{H}_0: \beta_{\text{farmer}} = \beta_{\text{int}}$)" "P-value ($ \text{H}_0: \beta_{\text{farmer}} = \beta_{\text{joint}}$)" "P-value ($ \text{H}_0: \beta_{\text{int}} = \beta_{\text{joint}}$)" "P-value ($ \text{H}_0: \beta_{\text{farmer}} + \beta_{\text{int}} = \beta_{\text{joint}}$)" "Control mean (Pass/Total)" "Control standard deviation" "R-squared" "Observations"))  ///
		label mlabels(none) collabels(none) nonotes nonumbers posthead("`titles'" "`numbers'") ///
		mgroups("Audit report on Compliance", pattern(1 0 0 0 0 0) ///
		span prefix(\multicolumn{@span}{c}{) suffix(}) erepeat(\cmidrule(lr){@span})) ///
		;


	local notes_gap_round \item Notes: This table reports treatment effects on farmer's GAP compliance. ///
	The results use data from two follow-up survey rounds: panel A shows the results from first follow-up survey and panel B shows the results from second follow-up survey. ///
	Dependent variables are standardized by the control group's mean and standard deviation. ///
	Column 1 uses total audit score which is the number of items passed across all 32 items. ///
	Columns 2-5 use number of items passed in each management category. ///
	All specifications include farmer and intermediary characteristics at baseline as control variables as well as strata fixed effects. ///
	Standard errors are clustered by farmer group and reported in parentheses. P-values from randomization inference are reported in square brackets. * denotes false discovery rate controlled statistical significance at 0.10, ** at 0.05, and *** at 0.01.; 		
		
		
** Estimation - First Survey;
	local outcome "score_fu1 pestman_fu1 equipman_fu1 hygman_fu1 landman_fu1 fertman_fu1";

		foreach y of local outcome {;
	
		eststo: qui reg std_`y' tr_farm tr_int tr_joint certelig tr_fXcert tr_iXcert tr_jXcert `balance' `intbalance' i.strata if round == 1, robust cluster(group_id);
		estadd ysumm;
		qui test tr_farm = tr_int;
		qui estadd scalar betafi = r(p);		
		qui test tr_farm = tr_joint;
		qui estadd scalar betafj = r(p);
		qui test tr_int = tr_joint;
		qui estadd scalar betaij = r(p);		
		qui test tr_farm+tr_int = tr_joint;
		qui estadd scalar betafij = r(p);
		qui sum audit_`y' if dum_tgroup1 == 1 & round == 1;
		qui estadd scalar ymean_control = r(mean);
		qui estadd scalar ysd_control = r(sd);
		};
	
** Table;
	loc colnum 6;
	local caption Impacts of Training and Certificate Eligibility on GAP Compliance - After 6 Months;
	local label audit_result_fu1;
	local notes `notes_gap_round';

head_foot, caption(`caption') label(`label') notes(`notes') columns(`colnum');

esttab  ///;
using "${table_path}/reg_audit_area_fu1.tex", replace ///
	keep(tr_farm tr_int tr_joint certelig tr_fXcert tr_iXcert tr_jXcert) `reg_table' ///
	prehead("$header") postfoot("$footer") substitute("\_" "_");

est clear;

** Estimation - Second Survey;
	local outcome "score_fu2 pestman_fu2 equipman_fu2 hygman_fu2 landman_fu2 fertman_fu2";

		foreach y of local outcome {;
	
		eststo: qui reg std_`y' tr_farm tr_int tr_joint certelig tr_fXcert tr_iXcert tr_jXcert `balance' `intbalance' i.strata if round == 2, robust cluster(group_id);
		estadd ysumm;
		qui test tr_farm = tr_int;
		qui estadd scalar betafi = r(p);		
		qui test tr_farm = tr_joint;
		qui estadd scalar betafj = r(p);
		qui test tr_int = tr_joint;
		qui estadd scalar betaij = r(p);		
		qui test tr_farm+tr_int = tr_joint;
		qui estadd scalar betafij = r(p);
		qui sum audit_`y' if dum_tgroup1 == 1 & round == 2;
		qui estadd scalar ymean_control = r(mean);
		qui estadd scalar ysd_control = r(sd);
		};
	
** Table;
	loc colnum 6;
	local caption Impacts of Training and Certificate on Farm Management by Area - After 12 Months;
	local label audit_result_fu2;
	local notes `notes_gap_round';

head_foot, caption(`caption') label(`label') notes(`notes') columns(`colnum');

esttab  ///;
using "${table_path}/reg_audit_area_fu2.tex", replace ///
	keep(tr_farm tr_int tr_joint certelig tr_fXcert tr_iXcert tr_jXcert) `reg_table' ///
	prehead("$header") postfoot("$footer") substitute("\_" "_");

est clear;
clear;


/*-----------------------------------------------------------------------------------
			OUTCOME = GAP Compliance, Heterogeneity Analysis
-----------------------------------------------------------------------------------*/

use `maindata';
	
	gen size_farm = ha_whitedragon;
	gen save_bank = save_bank_base;
	gen business = 5-mean_business_base;
	gen entrepre = 4-mean_confidence_base;
	gen present = present_bias_base;

	
*** Define local variables;
	
	local dummy_coef dum_tgroup2 dum_tgroup3 dum_tgroup4 dum_tgroup5 dum_tgroup6 dum_tgroup7 dum_tgroup8;
	local treat_coef tr_farm tr_int tr_joint certelig tr_fXcert tr_iXcert tr_jXcert;


	local balance age female edu_second exp_dragon size_farm white_age_less1 white_age_less3 white_age_less10 white_age_less20 white_age_more20 anycert_base loan_any_base save_bank mean_trust_base business entrepre raven_total present risky_invest gap_comply_base hours_base totalvolume_base price_base log_cost_fert_base log_cost_pest_base log_cost_facequip_base log_cost_labor_base log_cost_utility_base;
	
	local intbalance int_age int_size_facility int_volume_china_fu0 int_volume_asia_fu0 int_volume_eu_fu0 int_volume_dom_fu0 int_dum_firmtype1 int_dum_firmtype2 int_dum_firmtype3;

	
	
*** Generate heterogeneity variables;
	local hetero "female edu_second exp_dragon size_farm save_bank business entrepre present risky_invest raven_total";
	
	foreach z of local treat_coef {;
			gen `z'Xhet= .;
	};	
	
	local high "exp_dragon size_farm business entrepre risky_invest raven_total";
	foreach x of local high {;
		qui summ `x' if round ==1, detail;
		local med = r(p50);
		gen high_`x' = (`x'>= `med');
	};
	
	foreach y of local hetero {;
		foreach z of local treat_coef {;
			gen `z'X`y'= `z'*`y';
	};
	};
	
	foreach y of local high {;
		foreach z of local treat_coef {;
			replace `z'X`y'= `z'*high_`y';
	};
	};	
	
*** Generate heterogeneity variables for intermediary export market;
	gen int_export_china_fu0 = (int_volume_china_fu0 > 0);
	gen int_export_asia_fu0 = (int_volume_asia_fu0 > 0);
	gen int_export_eu_fu0 = (int_volume_eu_fu0 > 0);
	gen int_export_hp_fu0 = (int_export_asia_fu0 == 1 | int_export_eu_fu0 == 1);
	gen int_volume_hp_fu0 = int_volume_asia_fu0 + int_volume_eu_fu0;
	gen int_export_hp = (int_export_asian > 0 | int_export_other > 0 );	
	
	local treat_coef tr_farm tr_int tr_joint certelig tr_fXcert tr_iXcert tr_jXcert;
	local market "int_export_asia_fu0 int_export_eu_fu0 int_export_hp_fu0";
	
		foreach y of local market {;
			foreach z of local treat_coef {;
				gen `z'X`y'= `z'*`y';
			};
		};
	local market "int_volume_asia_fu0 int_volume_eu_fu0 int_volume_hp_fu0";
	foreach y of local market {;
			loc mar = substr("`y'",5,.);
			gen log_`mar' = log(`y'+1);
	foreach z of local treat_coef {;
			gen `z'Xlog_`mar'= `z'*log_`mar';
	};	
	};

	local export "int_export_asian int_export_other int_export_hp";
	
		foreach y of local export {;
			foreach z of local treat_coef {;
				gen `z'X`y'= `z'*`y';
			};
		};
		
	local int_char "int_age int_size_facility int_ghp_score_fu0 int_ghp_score_fu1";

		foreach y of local int_char {;
			foreach z of local treat_coef {;
				gen `z'X`y'= `z'*`y';
			};
			loc char = substr("`y'",5,.);
			qui summ `y' if round ==1, detail;
			loc med = r(p50);
			gen high_`char' = (`y'>= `med');
			foreach z of local treat_coef {;
				gen `z'Xhigh_`char'= `z'*high_`char';
			};
		};
		
		foreach z of local treat_coef {;
			gen `z'Xdist_jointtrader = `z'*dist_jointtrader;
		};
	
/*-----------------------------------------------------------------------------------
			 		Label 
-----------------------------------------------------------------------------------*/
	lab var tr_farmXhet "Farmer Training $\times$ Characteristic";
	lab var tr_intXhet "Exporter Training $\times$ Characteristic";
	lab var tr_jointXhet "Joint Training $\times$ Characteristic";
	lab var certeligXhet "Certificate Eligibility $\times$ Characteristic";
	lab var tr_fXcertXhet " $\times$ Farmer Training";
	lab var tr_iXcertXhet " $\times$ Exporter Training";
	lab var tr_jXcertXhet " $\times$ Joint Training";


/*-----------------------------------------------------------------------------------
			OUTCOME = GAP Compliance, Heterogeneity according to preanalysis plan (Online Appendix Table A-5)
-----------------------------------------------------------------------------------*/
	
** Setting;
	loc titles1 " \multicolumn{1}{r}{Characteristic:} & Female & Secondary & Farming & Size of & Savings & Business & Entrepre & Present & Risk & Raven's \\ ";
	loc titles2 "\multicolumn{1}{r}{(indicator var.)} & & Education & Experience & Farm & at bank & attitude & neurship & bias & taking & test \\ ";
	loc titles3 "& &  & ($\geq$ median) & ($\geq$ median) & & ($\geq$ median) & ($\geq$ median) & & ($\geq$ median) & ($\geq$ median) \\ ";	
	loc numbers "& (1) & (2) & (3) & (4) & (5) & (6) & (7) & (8) & (9) & (10) \\ \midrule";
	loc reg_table cells(b(fmt(3) star) se(par fmt(3))) starlevels(* 0.10 ** 0.05 *** 0.01) ///
		stats(r2 N, fmt(2 0) ///
		labels("R-squared" "Observations"))  ///
		label mlabels(none) collabels(none) nonotes nonumbers posthead("`titles1'" "`titles2'" "`titles3'" "`numbers'") ///
		mgroups("Outcome variable: Total Score on GAP Compliance", pattern(1 0 0 0 0 0 0 0 0 0) ///
		span prefix(\multicolumn{@span}{c}{) suffix(}) erepeat(\cmidrule(lr){@span})) ///
		;

	local notes_heterogeneity Notes: Audit on GAP compliance were conducted in each of the two follow-up survey rounds. Audit scores are standardized by the control group's mean and standard deviation. Each column reports coefficients on interaction terms between training treatment indicator and individual characteristic. For interacting, farming experience, size of farm, business attitude, entrepreneurship, risk taking, raven's test are constructed as indicator variables equal to one if the value is above the sample median. All specifications include farmer and exporter characteristics at baseline as control variables as well as strata fixed effects and survey round fixed effects. Standard errors are clustered by farmer group and reported in parentheses. * denotes  statistical significance at 0.10, ** at 0.05, and *** at 0.01.;
		
** Estimation;
	foreach y of local hetero {;
	foreach z of local treat_coef {;
			replace `z'Xhet = `z'X`y';
	};
	loc exclude `y';
	loc bal: list balance - exclude;
	loc treat_het "tr_farmXhet tr_intXhet tr_jointXhet certeligXhet tr_fXcertXhet tr_iXcertXhet tr_jXcertXhet";

		eststo: reg std_score `treat_coef' `treat_het' `bal' `intbalance' i.strata i.round, robust cluster(group_id);
		estadd ysumm;
		qui sum audit_score if dum_tgroup1 == 1;
		qui estadd scalar ymean_control = r(mean);

};
			

** Table;
	loc colnum 10;
	local caption Heterogeneous Impacts of Training and Certificate Eligibility;
	local label reg_heterogeneity;
	local notes `notes_heterogeneity';

head_foot, caption(`caption') label(`label') notes(`notes') columns(`colnum');

esttab  ///;
using "${table_path}/reg_heterogeneity2.tex", replace ///
	keep(tr_farm tr_int tr_joint tr_farmXhet tr_intXhet tr_jointXhet certelig certeligXhet tr_fXcert tr_iXcert tr_jXcert) `reg_table' ///
	prehead("$header") postfoot("$footer") substitute("\_" "_");

est clear;


/*-----------------------------------------------------------------------------------
			OUTCOME = GAP Compliance, Heterogeneity w.r.t exporter market (Online Appendix Table A-6)
-----------------------------------------------------------------------------------*/
	
	local market "int_export_asia_fu0 int_export_hp_fu0 ";
	local volume "int_volume_asia_fu0 int_volume_hp_fu0";
	local export "int_export_asian int_export_hp";
	local int_char "int_age int_size_facility int_ghp_score_fu0 int_ghp_score_fu1";	
	local high_char "high_age high_size_facility";	

** Setting;
	loc titles1 " \multicolumn{1}{r}{Characteristic:} & Age & Size & Export & Export  \\ ";
	loc titles2 "\multicolumn{1}{r}{(indicator var.)} & & Facility & volume & volume \\ ";
	loc titles3 "& ($\geq$ median) & ($\geq$ median) & Asia & Asia/EU \\ \cmidrule(lr){2-5}";	
	loc numbers "& (1) & (2) & (3) & (4) \\ \midrule";
	loc reg_table cells(b(fmt(3) star) se(par fmt(3))) starlevels(* 0.10 ** 0.05 *** 0.01) ///
		stats(r2 N, fmt(2 0) ///
		labels("R-squared" "Observations"))  ///
		label mlabels(none) collabels(none) nonotes nonumbers posthead("`titles1'" "`titles2'" "`titles3'" "`numbers'") ///
		mgroups("Outcome variable: GAP Compliance", pattern(1 0 0 0) ///
		span prefix(\multicolumn{@span}{c}{) suffix(}) erepeat(\cmidrule(lr){@span})) ///
		;	
	
	loc treat_het "tr_farmXhet tr_intXhet tr_jointXhet certeligXhet tr_fXcertXhet tr_iXcertXhet tr_jXcertXhet";
** Estimation;

	foreach y of local high_char {;
	foreach z of local treat_coef {;
			replace `z'Xhet = `z'X`y';
	};

	loc exclude `y';
	loc intbal: list intbalance - exclude;

	eststo: qui reg std_score `treat_coef' `treat_het' `balance' `intbal' i.strata i.round, robust cluster(group_id);
		
	};	


**** For market volume;
	foreach y of local volume {;
			loc mar = substr("`y'",5,.);
	foreach z of local treat_coef {;
			replace `z'Xhet = `z'Xlog_`mar';
	};


	loc exclude `y';
	loc intbal: list intbalance - exclude;

	eststo: qui reg std_score `treat_coef' `treat_het' `balance' `intbal' i.strata i.round, robust cluster(group_id);
		
	};		
	
** Table;
	loc colnum 4;
	local caption Heterogeneous Impacts by Exporter Characteristics;
	local label reg_heterogeneity_intermediarychar;
	local notes `notes_heterogeneity';

head_foot, caption(`caption') label(`label') notes(`notes') columns(`colnum');

esttab  ///;
using "${table_path}/reg_heterogeneity_intermediarychar.tex", replace ///
	keep(tr_farm tr_int tr_joint tr_farmXhet tr_intXhet tr_jointXhet certelig certeligXhet tr_fXcert tr_iXcert tr_jXcert) `reg_table' ///
	prehead("$header") postfoot("$footer") substitute("\_" "_");

est clear;
clear;


/*-----------------------------------------------------------------------------------
			 		OUTCOME = KNOWLEDGE, AWARENESS (Online Appendix Table A-7)
-----------------------------------------------------------------------------------*/

use `maindata';

	local balance age female edu_second exp_dragon ha_whitedragon white_age_less1 white_age_less3 white_age_less10 white_age_less20 white_age_more20 anycert_base loan_any_base save_bank_base mean_trust_base mean_business_base mean_confidence_base raven_total present_bias_base gap_comply_base hours_base totalvolume_base price_base log_cost_fert_base log_cost_pest_base log_cost_facequip_base log_cost_labor_base log_cost_utility_base;
	local intbalance int_age int_size_facility int_volume_china_fu0 int_volume_asia_fu0 int_volume_eu_fu0 int_volume_dom_fu0 int_dum_firmtype1 int_dum_firmtype2 int_dum_firmtype3;

** Setting;
	loc titles1 "& GAP & Food Safety & GAP & GHP \\ " ;
	loc titles2 "& Knowledge & Awareness & Knowledge & Compliance\\ \cmidrule{2-5}";
	loc numbers "& (1) & (2) & (3) & (4) \\ \midrule";
	loc reg_table cells(b(fmt(3) star) se(par fmt(3))) starlevels(* 0.10 ** 0.05 *** 0.01) ///
		stats(betafi betafj betaij betafij ymean_control ysd_control r2 N, fmt(2 2 2 2 2 2 2 0) ///
		labels("P-value ($ \text{H}_0: \beta_{\text{farmer}} = \beta_{\text{int}}$)" "P-value ($ \text{H}_0: \beta_{\text{farmer}} = \beta_{\text{joint}}$)" "P-value ($ \text{H}_0: \beta_{\text{int}} = \beta_{\text{joint}}$)" "P-value ($ \text{H}_0: \beta_{\text{farmer}} + \beta_{\text{int}} = \beta_{\text{joint}}$)" "Mean(Dep Var)" "SD(Dep Var)" "R-squared" "Observations"))  ///
		label mlabels(none) collabels(none) nonotes nonumbers posthead("`titles1'" "`titles2'" "`numbers'") ///
		mgroups("Farmer" "Exporter", pattern(1 0 1 0) ///
		span prefix(\multicolumn{@span}{c}{) suffix(}) erepeat(\cmidrule(lr){@span})) ///		
		;


	local notes_knowledge \item Notes: This table reports treatment effects on farmer's knowledge of GAP, awareness of food safety, intermediary's GAP knowledge, and compliance to Good Handling Practices (GHP). ///
	The results use data from two follow-up survey rounds. Dependent variables are standardized by the control group's mean and standard deviation. ///
 	Knowledge is constructed as standardized score on test consisting of 10 multiple choice questions conducted during first follow-up survey round. ///
	Awareness is a self-report on pesticide use and food safety asked during second follow-up survey found (higher score indicates farmer has greater awareness on food safety issues). ///
	All specifications include farmer and intermediary characteristics at baseline as control variables as well as strata and survey round fixed effects. ///
	Standard errors are clustered by farmer group and reported in parentheses. P-values from randomization inference are reported in square brackets. * denotes false discovery rate controlled statistical significance at 0.10, ** at 0.05, and *** at 0.01.; 		

** Estimation;
	local outcome "knowledge perception";

		foreach y of local outcome {;
	
		eststo: qui reg std_`y' tr_farm tr_int tr_joint certelig tr_fXcert tr_iXcert tr_jXcert `balance' `intbalance' i.strata if round == 2, robust cluster(group_id);
		estadd ysumm;
		qui test tr_farm = tr_int;
		qui estadd scalar betafi = r(p);		
		qui test tr_farm = tr_joint;
		qui estadd scalar betafj = r(p);
		qui test tr_int = tr_joint;
		qui estadd scalar betaij = r(p);		
		qui test tr_farm+tr_int = tr_joint;
		qui estadd scalar betafij = r(p);
		qui sum raw_`y' if dum_tgroup1 == 1 & round == 2;
		qui estadd scalar ymean_control = r(mean);
		qui estadd scalar ysd_control = r(sd);		
		};		

preserve;
clear;

*** Read data;
	
	use "${data_path}/INTERMEDIARY_LONG", replace;
	
	
*** Generate dummy for treatment groups;	
	
	tab st_tgroup, gen(dum_tgroup);
	egen group_id = group(strata st_tgroup);

*** Generate treatment levels and interaction terms;

	gen tr_farm = (st_tgroup == 2 | st_tgroup == 6);
	gen tr_int = (st_tgroup == 3 | st_tgroup == 7);
	gen tr_joint = (st_tgroup == 4 | st_tgroup == 8);
	gen certelig = (st_tgroup == 5 | st_tgroup == 6 | st_tgroup == 7 | st_tgroup == 8);
	gen tr_fXcert = tr_farm * certelig;
	gen tr_iXcert = tr_int * certelig;
	gen tr_jXcert = tr_joint * certelig; 
	
*** Define local variables;
	
	local dummy_coef dum_tgroup2 dum_tgroup3 dum_tgroup4 dum_tgroup5 dum_tgroup6 dum_tgroup7 dum_tgroup8;
	local treat_coef tr_fram tr_int tr_joint certelig tr_fXcert tr_iXcert tr_jXcert;

	local intermediary_balance "age dum_firmtype1 dum_firmtype2 dum_firmtype3 size_facility buy_avgprice_fu0 sell_avgprice_fu0 ghp_score_fu0 sell_contract_fu0 sell_exp_fu0 volume_china_fu0 volume_asia_fu0 volume_eu_fu0 volume_dom_fu0";
	
/*-----------------------------------------------------------------------------------
			 		Label 
-----------------------------------------------------------------------------------*/
	lab var tr_farm "Farmer Training";
	lab var tr_int "Exporter Training";
	lab var tr_joint "Joint Training";
	lab var certelig "Certificate Eligibility (C.E.)";
	lab var tr_fXcert "C.E. $\times$ Farmer Training";
	lab var tr_iXcert "C.E. $\times$ Exporter Training";
	lab var tr_jXcert "C.E $\times$ Joint Training";
	
	lab var age "Years of intermediation business";
	lab var size_facility "Size of packing/collection facility (square meters)";
	lab var dum_firmtype1 "Intermediary is collector";
	lab var dum_firmtype2 "Intermediary is export enterprise";
	lab var dum_firmtype3 "Intermediary is cooperative";
	lab var trade_volume_fu0 "Trade volume (tons)";
	lab var buy_avgprice_fu0 "Average purchase price (1,000 VND/kg)";
	lab var sell_avgprice_fu0 "Average sales price (1,000 VND/kg)";
	lab var ghp_score_fu0 "Self-reported GAP compliance";
	lab var sell_contract_fu0 "Contract with buyer";
	lab var sell_exp_fu0 "Years of experience with buyer";
	lab var volume_china_fu0 "Volume of Chinese exports (tons)";
	lab var volume_asia_fu0 "Volume of Asian exports (tons)";
	lab var volume_eu_fu0 "Volume of EU/US exports (tons)";
	lab var volume_dom_fu0 "Volume of domestic market (tons)";
	gen gaptest_fu1 = gaptest_score_fu1;
	gen score_fu1 = ghp_score_fu1;

** Estimation;
	local outcome "gaptest_fu1 score_fu1";

		foreach y of local outcome {;
	
		eststo: qui reg std_`y' tr_farm tr_int tr_joint certelig tr_fXcert tr_iXcert tr_jXcert `intermediary_balance' i.strata if round == 1, robust cluster(group_id);
		estadd ysumm;
		qui test tr_farm = tr_int;
		qui estadd scalar betafi = r(p);		
		qui test tr_farm = tr_joint;
		qui estadd scalar betafj = r(p);
		qui test tr_int = tr_joint;
		qui estadd scalar betaij = r(p);		
		qui test tr_farm+tr_int = tr_joint;
		qui estadd scalar betafij = r(p);			
		qui sum `y' if dum_tgroup1 == 1 & round == 1;
		qui estadd scalar ymean_control = r(mean);
		qui estadd scalar ysd_control = r(sd);
		};
		
restore;
		
** Table;
	loc colnum 5;
	local caption Impacts of Training and Certificate Eligibility on GAP Knowledge;
	local label knowledge_result_pool;
	local notes `notes_knowledge';

head_foot, caption(`caption') label(`label') notes(`notes') columns(`colnum');

esttab  ///;
using "${table_path}/reg_knowledge_pool2.tex", replace ///
	keep(tr_farm tr_int tr_joint certelig tr_fXcert tr_iXcert tr_jXcert) `reg_table' ///
	prehead("$header") postfoot("$footer") substitute("\_" "_");

est clear;
clear;
	

/*-----------------------------------------------------------------------------------
			 		OUTCOME = COST RESULTS (Online Appendix Table A-8)
-----------------------------------------------------------------------------------*/	

use `maindata';

** Setting;
	loc titles "& Total & Pesticide & Facility & Equipment & Fertilizer & Labor & Utility & Hours \\ \cmidrule{2-9}";
	loc numbers "& (1) & (2) & (3) & (4) & (5) & (6) & (7) & (8) \\ \midrule";
	loc reg_table cells(b(fmt(3) star) se(par fmt(3))) starlevels(* 0.10 ** 0.05 *** 0.01) ///
		stats(betafi betafj betaij betafij ymean_control r2 N, fmt(2 2 2 2 2 2 0) ///
		labels("P-value ($ \text{H}_0: \beta_{\text{farmer}} = \beta_{\text{int}}$)" "P-value ($ \text{H}_0: \beta_{\text{farmer}} = \beta_{\text{joint}}$)" "P-value ($ \text{H}_0: \beta_{\text{int}} = \beta_{\text{joint}}$)" "P-value ($ \text{H}_0: \beta_{\text{farmer}} + \beta_{\text{int}} = \beta_{\text{joint}}$)" "Control mean (in levels)" "R-squared" "Observations"))  ///
		label mlabels(none) collabels(none) nonotes nonumbers posthead("`titles'" "`numbers'") ///
		mgroups("Log transformed", pattern(1 0 0 0 0 0 0 1) ///
		span prefix(\multicolumn{@span}{c}{) suffix(}) erepeat(\cmidrule(lr){@span})) ///
		;
		*indicate("Control=`control'" "Enumerator FE=*.lottery_enu" "Survey center FE=*.svy_center" "Week FE=*.weeks" "Day FE=*.days");

	local notes_cost_round \item Notes: This table reports treatment effects on farm input costs. ///
	The results use data from two follow-up survey rounds: panel A shows the results from first follow-up survey and panel B shows the results from second follow-up survey. ///
	All input costs, except respondent's work hour, are log transformed. ///
    Total is the sum of input costs and cost specified as other in the survey. ///
	All specifications include farmer and intermediary characteristics at baseline as control variables as well as strata fixed effects. Standard errors are clustered by farmer group and reported in parentheses. ///
	P-values from randomization inference are reported in square brackets. * denotes false discovery rate controlled statistical significance at 0.10, ** at 0.05, and *** at 0.01.; 		
		
		
set seed 1357;
gen randnum = runiform();
drop if price == .;
drop if totalvolume == .;
drop if cost_costtotal == .;
drop if log_profit == .;		
		

** Estimation - First round survey;

		preserve;
		keep if round == 1;
		sort log_costtotal_fu1 randnum;
		drop if _n <= 5 | _n > _N-5;

	local outcome "costtotal_fu1 pest_fu1 facility_fu1 equip_fu1 fert_fu1 labor_fu1 utility_fu1";

		foreach y of local outcome {;
	
		eststo: qui reg log_`y' tr_farm tr_int tr_joint certelig tr_fXcert tr_iXcert tr_jXcert `balance' `intbalance' i.strata if round == 1, robust cluster(group_id);
		estadd ysumm;
		qui test tr_farm = tr_int;
		qui estadd scalar betafi = r(p);		
		qui test tr_farm = tr_joint;
		qui estadd scalar betafj = r(p);
		qui test tr_int = tr_joint;
		qui estadd scalar betaij = r(p);		
		qui test tr_farm+tr_int = tr_joint;
		qui estadd scalar betafij = r(p);
		qui sum cost_`y' if dum_tgroup1 == 1;
		qui estadd scalar ymean_control = r(mean);
		};
		
	local outcome "hours_fu1";

		foreach y of local outcome {;
	
		eststo: qui reg `y' tr_farm tr_int tr_joint certelig tr_fXcert tr_iXcert tr_jXcert `balance' `intbalance' i.strata if round == 1, robust cluster(group_id);
		estadd ysumm;
		qui test tr_farm = tr_int;
		qui estadd scalar betafi = r(p);		
		qui test tr_farm = tr_joint;
		qui estadd scalar betafj = r(p);
		qui test tr_int = tr_joint;
		qui estadd scalar betaij = r(p);		
		qui test tr_farm+tr_int = tr_joint;
		qui estadd scalar betafij = r(p);
		qui sum `y' if dum_tgroup1 == 1;
		qui estadd scalar ymean_control = r(mean);
		};
		

	
** Table;
	loc colnum 9;
	local caption Impacts of Training and Certificate Eligibility on Farm Input Costs - Round 1;
	local label input_result_fu1;
	local notes `notes_cost_round';

head_foot, caption(`caption') label(`label') notes(`notes') columns(`colnum');

esttab  ///;
using "${table_path}/reg_inputcost_fu1.tex", replace ///
	keep(tr_farm tr_int tr_joint certelig tr_fXcert tr_iXcert tr_jXcert) `reg_table' ///
	prehead("$header") postfoot("$footer") substitute("\_" "_");

est clear;
		restore;

** Estimation - Second round survey;

		preserve;
		keep if round == 2;
		sort log_costtotal_fu2 randnum;
		drop if _n <= 5 | _n > _N-5;

	local outcome "costtotal_fu2 pest_fu2 facility_fu2 equip_fu2 fert_fu2 labor_fu2 utility_fu2";

		foreach y of local outcome {;
	
		eststo: qui reg log_`y' tr_farm tr_int tr_joint certelig tr_fXcert tr_iXcert tr_jXcert `balance' `intbalance' i.strata if round == 2, robust cluster(group_id);
		estadd ysumm;
		qui test tr_farm = tr_int;
		qui estadd scalar betafi = r(p);		
		qui test tr_farm = tr_joint;
		qui estadd scalar betafj = r(p);
		qui test tr_int = tr_joint;
		qui estadd scalar betaij = r(p);		
		qui test tr_farm+tr_int = tr_joint;
		qui estadd scalar betafij = r(p);
		qui sum cost_`y' if dum_tgroup1 == 1;
		qui estadd scalar ymean_control = r(mean);
		};
		
	local outcome "hours_fu2";

		foreach y of local outcome {;
	
		eststo: qui reg `y' tr_farm tr_int tr_joint certelig tr_fXcert tr_iXcert tr_jXcert `balance' `intbalance' i.strata if round == 2, robust cluster(group_id);
		estadd ysumm;
		qui test tr_farm = tr_int;
		qui estadd scalar betafi = r(p);		
		qui test tr_farm = tr_joint;
		qui estadd scalar betafj = r(p);
		qui test tr_int = tr_joint;
		qui estadd scalar betaij = r(p);		
		qui test tr_farm+tr_int = tr_joint;
		qui estadd scalar betafij = r(p);
		qui sum `y' if dum_tgroup1 == 1;
		qui estadd scalar ymean_control = r(mean);
		};
	
** Table;
	loc colnum 9;
	local caption Impacts of Training and Certificate Eligibility on Farm Input Costs - Round 2;
	local label input_result_fu2;
	local notes `notes_cost_round';

head_foot, caption(`caption') label(`label') notes(`notes') columns(`colnum');

esttab  ///;
using "${table_path}/reg_inputcost_fu2.tex", replace ///
	keep(tr_farm tr_int tr_joint certelig tr_fXcert tr_iXcert tr_jXcert) `reg_table' ///
	prehead("$header") postfoot("$footer") substitute("\_" "_");

est clear;
restore;
clear;

/*-----------------------------------------------------------------------------------
			 		OUTCOME = PRODUCT ATTRIBUTES (Online Appendix Table A-9)
-----------------------------------------------------------------------------------*/	

use `maindata';

local balance age female edu_second exp_dragon ha_whitedragon white_age_less1 white_age_less3 white_age_less10 white_age_less20 white_age_more20 anycert_base loan_any_base save_bank_base mean_trust_base mean_business_base mean_confidence_base raven_total present_bias_base gap_comply_base hours_base totalvolume_base price_base log_cost_fert_base log_cost_pest_base log_cost_facequip_base log_cost_labor_base log_cost_utility_base;

local intbalance int_age int_size_facility int_volume_china_fu0 int_volume_asia_fu0 int_volume_eu_fu0 int_volume_dom_fu0 int_dum_firmtype1 int_dum_firmtype2 int_dum_firmtype3;
	
** Setting;
	loc titles1 "& Overall & \multicolumn{6}{c}{Individual Product Attributes} \\ \cmidrule{3-8}";
	loc titles2 "& Index & Sweetness & Skin & Bract & Length & Width & Weight \\ \cmidrule{2-8}";
	loc numbers "& (1) & (2) & (3) & (4) & (5) & (6) & (7) \\ \midrule";
	loc reg_table cells(b(fmt(3) star) se(par fmt(3))) starlevels(* 0.10 ** 0.05 *** 0.01) ///
		stats(betafj ymean_control ysd_control r2 N, fmt(2 2 2 2 0) ///
		labels("P-value ($ \text{H}_0: \beta_{\text{farmer}} = \beta_{\text{joint}}$)" "Control mean (in raw units)" "Control standard deviation" "R-squared" "Observations"))  ///
		label mlabels(none) collabels(none) nonotes nonumbers posthead("`titles1'" "`titles2'" "`numbers'") ///
		;

	local notes_attribute \item Notes: This table reports treatment effects on product attributes. ///
	The results use data from two follow-up survey rounds. Each product attribute is standardized by the control group's mean and standard deviation. ///
	Column 1 uses the average of the six standardized product attributes. All specifications include farmer and intermediary characteristics at baseline as control variables as well as strata and survey round fixed effects. /// 
	Standard errors are clustered by farmer group and reported in parentheses. P-values from randomization inference are reported in square brackets. * denotes false discovery rate controlled statistical significance at 0.10, ** at 0.05, and *** at 0.01.; 		
		
		
** Estimation;
	local outcome "meanquality sweet skin bract length width weight";

		foreach y of local outcome {;
	
		eststo: qui reg std_`y' tr_farm tr_int tr_joint certelig tr_fXcert tr_iXcert tr_jXcert `balance' `intbalance' i.strata i.round, robust cluster(group_id);
		estadd ysumm;
		qui test tr_farm = tr_joint;
		qui estadd scalar betafj = r(p);
		qui sum avg`y' if dum_tgroup1 == 1;
		qui estadd scalar ymean_control = r(mean);
		qui estadd scalar ysd_control = r(sd);
		};
	
** Table;
	loc colnum 7;
	local caption Impacts of Training and Certificate Eligibility on Product Attributes;
	local label attribute_result_pool;
	local notes `notes_attribute';

head_foot, caption(`caption') label(`label') notes(`notes') columns(`colnum');

esttab  ///;
using "${table_path}/reg_attribute_pool.tex", replace ///
	keep(tr_farm tr_int tr_joint certelig tr_fXcert tr_iXcert tr_jXcert) `reg_table' ///
	prehead("$header") postfoot("$footer") substitute("\_" "_");

est clear;
		
/*-----------------------------------------------------------------------------------
			 		OUTCOME = EXPORT PERFORMANCE (Online Appendix Table A-10, A-12)
-----------------------------------------------------------------------------------*/	


** Setting;
	loc titles "& China & High-price Asia & Domestic & EU/US \\ \cmidrule{2-5}";
	loc numbers "& (1) & (2) & (3) & (4) \\ \midrule";
	loc reg_table cells(b(fmt(3) star) se(par fmt(3))) starlevels(* 0.10 ** 0.05 *** 0.01) ///
		stats(betafi betafj betaij betafij ymean_control r2 N, fmt(2 2 2 2 2 2 0) ///
		labels("P-value ($ \text{H}_0: \beta_{\text{farmer}} = \beta_{\text{int}}$)" "P-value ($ \text{H}_0: \beta_{\text{farmer}} = \beta_{\text{joint}}$)" "P-value ($ \text{H}_0: \beta_{\text{int}} = \beta_{\text{joint}}$)" "P-value ($ \text{H}_0: \beta_{\text{farmer}} + \beta_{\text{int}} = \beta_{\text{joint}}$)" "Control mean (in levels)" "R-squared" "Observations"))  ///
		label mlabels(none) collabels(none) nonotes nonumbers posthead("`titles'" "`numbers'") ///
		mgroups("Log(Volume by Market)", pattern(1 0 0 0) ///
		span prefix(\multicolumn{@span}{c}{) suffix(}) erepeat(\cmidrule(lr){@span})) ///
		;


	local notes_export \item Notes: This table reports treatment effects on farmer product's market destination. ///
	The results use sales data from two follow-up survey rounds. The dependent variables in columns 1-4 use natural log of volume sold to each market. ///
	All specifications include farmer and intermediary characteristics at baseline as control variables as well as strata fixed effects. Standard errors are clustered by farmer group and reported in parentheses. ///
	P-values from randomization inference are reported in square brackets. * denotes false discovery rate controlled statistical significance at 0.10, ** at 0.05, and *** at 0.01.; 		

	
** Estimation - First survey;	
	local outcome "volume_china volume_asia volume_dom volume_eu";

		foreach y of local outcome {;
	
		eststo: qui reg log_`y' tr_farm tr_int tr_joint certelig tr_fXcert tr_iXcert tr_jXcert `balance' `intbalance' i.strata if round == 1, robust cluster(group_id);
		estadd ysumm;
		qui test tr_farm = tr_int;
		qui estadd scalar betafi = r(p);		
		qui test tr_farm = tr_joint;
		qui estadd scalar betafj = r(p);
		qui test tr_int = tr_joint;
		qui estadd scalar betaij = r(p);		
		qui test tr_farm+tr_int = tr_joint;
		qui estadd scalar betafij = r(p);	
		qui sum `y' if dum_tgroup1 == 1 & round == 1;
		qui estadd scalar ymean_control = r(mean);
		};
	
** Table;
	loc colnum 5;
	local caption Impacts of Training and Certificate Eligibility on Market Destination - Six months;
	local label export_result_fu1;
	local notes `notes_export';

head_foot, caption(`caption') label(`label') notes(`notes') columns(`colnum');

esttab  ///;
using "${table_path}/reg_export_volume_fu1.tex", replace ///
	keep(tr_farm tr_int tr_joint certelig tr_fXcert tr_iXcert tr_jXcert) `reg_table' ///
	prehead("$header") postfoot("$footer") substitute("\_" "_");

est clear;

** Estimation - Second survey;
	local outcome "volume_china volume_asia volume_dom volume_eu";

		foreach y of local outcome {;
	
		eststo: qui reg log_`y' tr_farm tr_int tr_joint certelig tr_fXcert tr_iXcert tr_jXcert `balance' `intbalance' i.strata if round == 2, robust cluster(group_id);
		estadd ysumm;
		qui test tr_farm = tr_int;
		qui estadd scalar betafi = r(p);		
		qui test tr_farm = tr_joint;
		qui estadd scalar betafj = r(p);
		qui test tr_int = tr_joint;
		qui estadd scalar betaij = r(p);		
		qui test tr_farm+tr_int = tr_joint;
		qui estadd scalar betafij = r(p);	
		qui sum `y' if dum_tgroup1 == 1 & round == 2;
		qui estadd scalar ymean_control = r(mean);
		};
	
** Table;
	loc colnum 5;
	local caption Impacts of Training and Certificate Eligibility on Market Destination - Twelve months;
	local label export_result_fu2;
	local notes `notes_export';

head_foot, caption(`caption') label(`label') notes(`notes') columns(`colnum');

esttab  ///;
using "${table_path}/reg_export_volume_fu2.tex", replace ///
	keep(tr_farm tr_int tr_joint certelig tr_fXcert tr_iXcert tr_jXcert) `reg_table' ///
	prehead("$header") postfoot("$footer") substitute("\_" "_");

est clear;
clear;


/*-----------------------------------------------------------------------------------

			 	EXPORTER ANALYSIS
					
-----------------------------------------------------------------------------------*/	


*** Read data;
	
	use "${data_path}/INTERMEDIARY_LONG", replace;
	
	
*** Generate dummy for treatment groups;	
	
	tab st_tgroup, gen(dum_tgroup);
	egen group_id = group(strata st_tgroup);

*** Generate treatment levels and interaction terms;

	gen tr_farm = (st_tgroup == 2 | st_tgroup == 6);
	gen tr_int = (st_tgroup == 3 | st_tgroup == 7);
	gen tr_joint = (st_tgroup == 4 | st_tgroup == 8);
	gen certelig = (st_tgroup == 5 | st_tgroup == 6 | st_tgroup == 7 | st_tgroup == 8);
	gen tr_fXcert = tr_farm * certelig;
	gen tr_iXcert = tr_int * certelig;
	gen tr_jXcert = tr_joint * certelig; 
	
*** Define local variables;
	
	local dummy_coef dum_tgroup2 dum_tgroup3 dum_tgroup4 dum_tgroup5 dum_tgroup6 dum_tgroup7 dum_tgroup8;
	local treat_coef tr_fram tr_int tr_joint certelig tr_fXcert tr_iXcert tr_jXcert;

	local intermediary_balance "age dum_firmtype1 dum_firmtype2 dum_firmtype3 size_facility buy_avgprice_fu0 sell_avgprice_fu0 ghp_score_fu0 sell_contract_fu0 sell_exp_fu0 volume_china_fu0 volume_asia_fu0 volume_eu_fu0 volume_dom_fu0";
	
/*-----------------------------------------------------------------------------------
			 		Label 
-----------------------------------------------------------------------------------*/
	lab var tr_farm "Farmer Training";
	lab var tr_int "Exporter Training";
	lab var tr_joint "Joint Training";
	lab var certelig "Certificate Eligibility";
	lab var tr_fXcert " $\times$ Farmer Training";
	lab var tr_iXcert " $\times$ Exporter Training";
	lab var tr_jXcert " $\times$ Joint Training";
	
	lab var age "Years of intermediation business";
	lab var size_facility "Size of packing/collection facility (square meters)";
	lab var dum_firmtype1 "Intermediary is collector";
	lab var dum_firmtype2 "Intermediary is export enterprise";
	lab var dum_firmtype3 "Intermediary is cooperative";
	lab var trade_volume_fu0 "Trade volume (tons)";
	lab var buy_avgprice_fu0 "Average purchase price (1,000 VND/kg)";
	lab var sell_avgprice_fu0 "Average sales price (1,000 VND/kg)";
	lab var ghp_score_fu0 "Self-reported GAP compliance";
	lab var sell_contract_fu0 "Contract with buyer";
	lab var sell_exp_fu0 "Years of experience with buyer";
	lab var volume_china_fu0 "Volume of Chinese exports (tons)";
	lab var volume_asia_fu0 "Volume of Asian exports (tons)";
	lab var volume_eu_fu0 "Volume of EU/US exports (tons)";
	lab var volume_dom_fu0 "Volume of domestic market (tons)";

	
tempfile traderdata;
save `traderdata';
	
 

/*-----------------------------------------------------------------------------------
			 		OUTCOME = EXPORTER SALES (Online Appendix Table A-13)
-----------------------------------------------------------------------------------*/

** Setting;
	loc titles "& Purchase & Sell & Total & Direct & Implied & Total & Direct & Implied \\ \cmidrule{2-9}";
	loc numbers "& (1) & (2) & (3) & (4) & (5) & (6) & (7) & (8) \\ \midrule";
	loc reg_table cells(b(fmt(3) star) se(par fmt(3))) starlevels(* 0.10 ** 0.05 *** 0.01) ///
		stats(betafi betafj betaij betafij ymean_control r2 N, fmt(2 2 2 2 2 2 0) ///
		labels("P-value ($ \text{H}_0: \beta_{\text{farmer}} = \beta_{\text{int}}$)" "P-value ($ \text{H}_0: \beta_{\text{farmer}} = \beta_{\text{joint}}$)" "P-value ($ \text{H}_0: \beta_{\text{int}} = \beta_{\text{joint}}$)" "P-value ($ \text{H}_0: \beta_{\text{farmer}} + \beta_{\text{int}} = \beta_{\text{joint}}$)" "Control mean" "R-squared" "Observations"))  ///
		label mlabels(none) collabels(none) nonotes nonumbers posthead("`titles'" "`numbers'") ///
		mgroups("Price" "Volume" "Revenue" "Cost" "Profit", pattern(1 0 1 1 0 1 1 0) ///
		span prefix(\multicolumn{@span}{c}{) suffix(}) erepeat(\cmidrule(lr){@span})) ///
		;

	local notes_intsales \item Notes: This table reports treatment effects on intermediary's sales performances.
	The results use data from two follow-up survey rounds with intermediaries. Direct revenue uses intermediary reports on revenue and Derived revenue is the product of (average facility gate price - average farm gate price) and total volume sold in the survey. Cost is measured along three areas - hired labor, utility, and material (excludes dragon fruit purchase). ///
	Profit in column 7 is derived by subtracting cost from direct revenue and profit in column 8 is derived by subtracting cost from implied revenue. ///
	All specifications include intermediary characteristics at baseline as control variables as well as strata fixed effects and survey round fixed effects. Standard errors are clustered by farmer-intermediary group and reported in parentheses. ///
	P-values from randomization inference are reported in square brackets. * denotes false discovery rate controlled statistical significance at 0.10, ** at 0.05, and *** at 0.01.; 		

** Estimation - Round 1;		
	local outcome "buy_avgprice sell_avgprice trade_volume totalrev implied_rev cost_total";

		foreach y of local outcome {;
	
		eststo: qui reg log_`y' tr_farm tr_int tr_joint certelig tr_fXcert tr_iXcert tr_jXcert `intermediary_balance' i.strata if round == 1, robust cluster(group_id);
		estadd ysumm;
		qui test tr_farm = tr_int;
		qui estadd scalar betafi = r(p);		
		qui test tr_farm = tr_joint;
		qui estadd scalar betafj = r(p);
		qui test tr_int = tr_joint;
		qui estadd scalar betaij = r(p);		
		qui test tr_farm+tr_int = tr_joint;
		qui estadd scalar betafij = r(p);			
		qui sum `y' if dum_tgroup1 == 1 & round == 1;
		qui estadd scalar ymean_control = r(mean);
		};
		
** Estimation;		
	local outcome "profit implied_prof";

		foreach y of local outcome {;
	
		eststo: qui reg `y' tr_farm tr_int tr_joint certelig tr_fXcert tr_iXcert tr_jXcert `intermediary_balance' i.strata if round == 1, robust cluster(group_id);
		estadd ysumm;
		qui test tr_farm = tr_int;
		qui estadd scalar betafi = r(p);		
		qui test tr_farm = tr_joint;
		qui estadd scalar betafj = r(p);
		qui test tr_int = tr_joint;
		qui estadd scalar betaij = r(p);		
		qui test tr_farm+tr_int = tr_joint;
		qui estadd scalar betafij = r(p);			
		qui sum `y' if dum_tgroup1 == 1 & round == 1;
		qui estadd scalar ymean_control = r(mean);
		};		
	
** Table;
	loc colnum 10;
	local caption Treatment Effects on Exporters' Sales and Profits - Round 1;
	local label intermediary_result_pool;
	local notes `notes_intsales';

head_foot, caption(`caption') label(`label') notes(`notes') columns(`colnum');

esttab  ///;
using "${table_path}/reg_intermediary_result_fu1.tex", replace ///
	keep(tr_farm tr_int tr_joint certelig tr_fXcert tr_iXcert tr_jXcert) `reg_table' ///
	prehead("$header") postfoot("$footer") substitute("\_" "_");

est clear;


** Estimation - Round 2;		
	local outcome "buy_avgprice sell_avgprice trade_volume totalrev implied_rev cost_total ";

		foreach y of local outcome {;
	
		eststo: qui reg log_`y' tr_farm tr_int tr_joint certelig tr_fXcert tr_iXcert tr_jXcert `intermediary_balance' i.strata if round == 2, robust cluster(group_id);
		estadd ysumm;
		qui test tr_farm = tr_int;
		qui estadd scalar betafi = r(p);		
		qui test tr_farm = tr_joint;
		qui estadd scalar betafj = r(p);
		qui test tr_int = tr_joint;
		qui estadd scalar betaij = r(p);		
		qui test tr_farm+tr_int = tr_joint;
		qui estadd scalar betafij = r(p);			
		qui sum `y' if dum_tgroup1 == 1 & round == 2;
		qui estadd scalar ymean_control = r(mean);
		};
		
** Estimation;		
	local outcome "profit implied_prof";

		foreach y of local outcome {;
	
		eststo: qui reg `y' tr_farm tr_int tr_joint certelig tr_fXcert tr_iXcert tr_jXcert `intermediary_balance' i.strata if round == 2, robust cluster(group_id);
		estadd ysumm;
		qui test tr_farm = tr_int;
		qui estadd scalar betafi = r(p);		
		qui test tr_farm = tr_joint;
		qui estadd scalar betafj = r(p);
		qui test tr_int = tr_joint;
		qui estadd scalar betaij = r(p);		
		qui test tr_farm+tr_int = tr_joint;
		qui estadd scalar betafij = r(p);			
		qui sum `y' if dum_tgroup1 == 1 & round == 2;
		qui estadd scalar ymean_control = r(mean);
		};		
	
** Table;
	loc colnum 10;
	local caption Treatment Effects on Exporters' Sales and Profits - Round 2;
	local label intermediary_result_pool;
	local notes `notes_intsales';

head_foot, caption(`caption') label(`label') notes(`notes') columns(`colnum');

esttab  ///;
using "${table_path}/reg_intermediary_result_fu2.tex", replace ///
	keep(tr_farm tr_int tr_joint certelig tr_fXcert tr_iXcert tr_jXcert) `reg_table' ///
	prehead("$header") postfoot("$footer") substitute("\_" "_");

est clear;



/*-----------------------------------------------------------------------------------
			 OUTCOME = EXPORT PRICE AND VOLUME BY MARKET (Online Appendix Table A-14)
-----------------------------------------------------------------------------------*/

** Setting;
	loc titles "& China & High-price Asia & US/EU \\ \cmidrule{2-4}";
	loc numbers "& (1) & (2) & (3) \\ \midrule";
	loc reg_table cells(b(fmt(3) star) se(par fmt(3))) starlevels(* 0.10 ** 0.05 *** 0.01) ///
		stats(ymean_control r2 N, fmt(2 2 0) ///
		labels("Control mean" "R-squared" "Observations"))  ///
		label mlabels(none) collabels(none) nonotes nonumbers posthead("`titles'" "`numbers'") ///
		mgroups("Log(Volume)", pattern(1 0 0) ///
		span prefix(\multicolumn{@span}{c}{) suffix(}) erepeat(\cmidrule(lr){@span})) ///
		;

	local notes_intmarket \item Notes: This table reports treatment effects on exporter's market destination.
	The results use data from two follow-up survey rounds with exporters.
	All specifications include exporter characteristics at baseline as control variables as well as strata fixed effects and survey round fixed effects. Standard errors are clustered by farmer-exporter group and reported in parentheses. ///
	P-values from randomization inference are reported in square brackets. * denotes false discovery rate controlled statistical significance at 0.10, ** at 0.05, and *** at 0.01.;			
		

** Estimation - Round 1;
		
	local outcome "volume_china volume_asia volume_eu";

		foreach y of local outcome {;
	
		eststo: qui reg log_`y' tr_farm tr_int tr_joint certelig tr_fXcert tr_iXcert tr_jXcert `intermediary_balance' i.strata if round == 1, robust cluster(group_id);
		estadd ysumm;
		qui sum `y' if dum_tgroup1 == 1 & round == 1;
		qui estadd scalar ymean_control = r(mean);
		};
	
** Table;
	loc colnum 3;
	local caption Treatment Effects on Exporters' Sales by Market - Round 1;
	local label intermediary_result_salesbymarket;
	local notes `notes_intmarket';

head_foot, caption(`caption') label(`label') notes(`notes') columns(`colnum');

esttab  ///;
using "${table_path}/reg_intermediary_salesbymarket_round1.tex", replace ///
	keep(tr_farm tr_int tr_joint certelig tr_fXcert tr_iXcert tr_jXcert) `reg_table' ///
	prehead("$header") postfoot("$footer") substitute("\_" "_");

est clear;

** Estimation - Round 2;
		
	local outcome "volume_china volume_asia volume_eu";

		foreach y of local outcome {;
	
		eststo: qui reg log_`y' tr_farm tr_int tr_joint certelig tr_fXcert tr_iXcert tr_jXcert `intermediary_balance' i.strata if round == 2, robust cluster(group_id);
		estadd ysumm;
		qui sum `y' if dum_tgroup1 == 1 & round == 2;
		qui estadd scalar ymean_control = r(mean);
		};
	
** Table;
	loc colnum 3;
	local caption Treatment Effects on Exporters' Sales by Market - Round 2;
	local label intermediary_result_salesbymarket;
	local notes `notes_intmarket';

head_foot, caption(`caption') label(`label') notes(`notes') columns(`colnum');

esttab  ///;
using "${table_path}/reg_intermediary_salesbymarket_round2.tex", replace ///
	keep(tr_farm tr_int tr_joint certelig tr_fXcert tr_iXcert tr_jXcert) `reg_table' ///
	prehead("$header") postfoot("$footer") substitute("\_" "_");

est clear;


/*-----------------------------------------------------------------------------------
			 		OUTCOME = Price by Market Destination (Online Appendix Table A-15)
-----------------------------------------------------------------------------------*/


use "${data_path}/INTERMEDIARY_COMBINETRADE_CLEAN", replace;
gen foll = "_fu0" if followup == 0;
replace foll = "_fu1" if followup == 1;
replace foll = "_fu2" if followup == 2;
drop followup;


merge m:1 traderid using "${data_path}/INTERMEDIARY_COMBINESURVEY_CLEAN";
drop _merge;
encode foll, gen(round);
tab st_tgroup, gen(dum_tgroup);
egen group_id = group(strata st_tgroup);


local intermediary_balance "age size_facility sell_exp trade_volume_fu0 cost_material_fu0 cost_labor_fu0 cost_utility_fu0 ghp_score_fu0";

gen buy_price = buy_avgprice_fu1*1000 if foll == "_fu1";
replace buy_price = buy_avgprice_fu2*1000 if foll == "_fu2";

lab var dum_market2 "High-price Asian markets";
lab var dum_market3 "US or EU markets";
lab var dum_market4 "Domestic markets";
gen log_buy_price = log(buy_price);
gen log_sell_price = log(sell_price);

** Setting;
	loc titles "& Log(Price sold to buyers)  & Log(Price paid to sellers) \\ \cmidrule{2-3}";
	loc numbers "& (1) & (2) \\ \midrule";
	loc reg_table cells(b(fmt(3) star) se(par fmt(3))) starlevels(* 0.10 ** 0.05 *** 0.01) ///
		stats(ymean_control r2 N, fmt(2 2 0) ///
		labels("Mean price for Chinese market" "R-squared" "Observations"))  ///
		label mlabels(none) collabels(none) nonotes nonumbers posthead("`titles'" "`numbers'") ;

	local notes_price_bymarket \item Notes: Column 1 reports estimates from regressing price exporter sold to buyer on buyer's market using transaction level data reported by exporters. Column 2 reports estimates from regressing price paid to sellers (either farmer or local collector) on exporter's share of volume sold to each market. We do not have data on price paid by exporter at the transaction level. The data is from two follow-up survey rounds. The baseline is mean price for Chinese markets. ///
	All specifications include exporter characteristics at baseline as control variables as well as strata fixed effects and survey round fixed effects. Standard errors are clustered by farmer-exporter group and reported in parentheses. * denotes statistical significance at 0.10, ** at 0.05, and *** at 0.01.; 		

** Estimation;		
	local outcome "sell_price";

		foreach y of local outcome {;
	
		eststo: qui reg log_`y' dum_market2-dum_market4 `intermediary_balance' i.strata i.round if foll != "_fu0", robust cluster(group_id);
		estadd ysumm;			
		qui sum `y' if dum_market1 == 1;
		qui estadd scalar ymean_control = r(mean);
		};		
	
	clear;
	
	use `traderdata';
	gen total_volume = volume_china + volume_asia + volume_eu + volume_dom;
	gen share_vol_china = volume_china/total_volume;
	gen share_vol_asia = volume_asia/total_volume;
	gen share_vol_eu = volume_eu/total_volume;
	gen share_vol_dom = volume_dom/total_volume;
	gen log_buy_price = log(buy_avgprice*1000);
	gen buy_price = buy_avgprice*1000;
	lab var share_vol_asia "Share of volume for High-price Asian markets";
	lab var share_vol_eu "Share of volume for US or EU markets";
	lab var share_vol_dom "Share of volume for domestic markets";
	gen dum_market2 = .;
	gen dum_market3 = .;
	gen dum_market4 = .;
	lab var dum_market2 "High-price Asian markets";
	lab var dum_market3 "US or EU markets";
	lab var dum_market4 "Domestic markets";
	
	local outcome "buy_price";

		foreach y of local outcome {;
	
		eststo: qui reg log_`y' share_vol_asia share_vol_eu share_vol_dom `intermediary_balance' i.strata i.round if foll != "_fu0", robust cluster(group_id);
		estadd ysumm;			
		qui sum `y' if share_vol_china == 1;
		qui estadd scalar ymean_control = r(mean);
		};		
	
** Table;
	loc colnum 2;
	local caption Exporter's Trade Price by Market;
	local label intermediary_price_bymarket;
	local notes `notes_price_bymarket';

head_foot, caption(`caption') label(`label') notes(`notes') columns(`colnum');

esttab  ///;
using "${table_path}/reg_intermediary_price_bymarket.tex", replace ///
	keep(dum_market2 dum_market3 dum_market4 share_vol_asia share_vol_eu share_vol_dom) `reg_table' ///
	prehead("$header") postfoot("$footer") substitute("\_" "_");

est clear;
clear;

/*-----------------------------------------------------------------------------------
			 		SALES TRANSACTION LEVEL ANALYSIS
-----------------------------------------------------------------------------------*/	

clear;
*** Read data;
	
	use "${data_path}/FARMER_COMBINETRADE_CLEAN", replace;

	rename followup round;

	merge m:1 farmerid round using "${data_path}/FARMER_LONG_SETUP";
	keep if _merge == 3;
	drop _merge;
	
*** Drop control communes;

	drop if control_commune == 1;	
	
*** Generate dummy for treatment groups;	
	
	tab st_tgroup, gen(dum_tgroup);
	
*** Cluster for standard errors;

	egen group_id = group(strata st_tgroup);

*** Drop large farms (farm size larger than five hectares);

	drop if ha_whitedragon >= 5;	
	
*** Generate treatment levels and interaction terms;

	gen tr_farm = (st_tgroup == 2 | st_tgroup == 6);
	gen tr_int = (st_tgroup == 3 | st_tgroup == 7);
	gen tr_joint = (st_tgroup == 4 | st_tgroup == 8);
	gen certelig = (st_tgroup == 5 | st_tgroup == 6 | st_tgroup == 7 | st_tgroup == 8);
	gen tr_fXcert = tr_farm * certelig;
	gen tr_iXcert = tr_int * certelig;
	gen tr_jXcert = tr_joint * certelig;
	gen iv_tr_farmer = (tr_farm == 1 | tr_joint == 1);
	gen control = (st_tgroup == 1 | st_tgroup == 5);
	
** Generate outcome variables;

	gen log_buyer_price = log(buyer_price);
	tab buyer_type, gen(dum_type);
	tab buyer_contract, gen(dum_contract);
	gen nonprogmeet = (1-progmeet);
	gen contract_within = (buyer_contract == 1 | buyer_contract == 2)*progmeet ;
	gen contract_outside = (buyer_contract == 1 | buyer_contract == 2)*nonprogmeet;
	gen spot_within = (buyer_contract == 3)*progmeet ;
	gen spot_outside = (buyer_contract == 3)*nonprogmeet ;
	replace contract_within = 0 if buyer_type == 1 ;
	replace contract_outside = 0 if buyer_type == 1 ;	
	replace spot_within = (buyer_type == 1)*progmeet ;
	replace spot_outside = (buyer_type == 1)*nonprogmeet ;
	
	local balance age female edu_second exp_dragon ha_whitedragon white_age_less1 white_age_less3 white_age_less10 white_age_less20 white_age_more20 anycert_base loan_any_base save_bank_base mean_trust_base mean_business_base mean_confidence_base raven_total present_bias_base gap_comply_base hours_base totalvolume_base price_base log_cost_fert_base log_cost_pest_base log_cost_facequip_base log_cost_labor_base log_cost_utility_base;
	local intbalance int_age int_size_facility int_volume_china_fu0 int_volume_asia_fu0 int_volume_eu_fu0 int_volume_dom_fu0 int_dum_firmtype1 int_dum_firmtype2 int_dum_firmtype3;
	
/*-----------------------------------------------------------------------------------
			 		Label 
-----------------------------------------------------------------------------------*/
	lab var age "Age";
	lab var female "Female";
	lab var edu_second "Secondary Education";
	lab var exp_dragon "Experience growing dragon fruit (years)";
	lab var ha_whitedragon "Size of dragon fruit farm (hectares)";
	lab var tree_whitedragon "Number of dragon fruit trees";
	lab var anycert_base "Received any agricultural certificate before";
	lab var loan_any_base "Ever received loan for farm investment";
	lab var save_bank_base "Ever saved at bank";
	lab var mean_trust_base "Mean score on trust measurement";
	lab var mean_business_base "Mean score on business measurement";
	lab var mean_confidence_base "Mean score on confidence measurement";
	lab var raven_total "Raven matrices score";
	lab var present_bias_base "Time discounting - present biased";
	lab var gap_comply_base "Self-reported GAP compliance";
	lab var dum_market1 "China";
	lab var dum_market2 "Asia (excluding China)";
	lab var dum_market3 "EU/US";
	lab var dum_market4 "Domestic";
	lab var std_score "GAP compliance";
	lab var std_sweet "Sweetness";
	lab var std_skin "Skin";
	lab var std_bract "Bract";
	lab var std_length "Length";
	lab var std_width "Width";
	lab var std_weight "Weight";
	lab var tr_farm "Farmer Training";
	lab var tr_int "Exporter Training";
	lab var tr_joint "Joint Training";
	lab var certelig "Certificate Eligibility (C.E.)";
	lab var tr_fXcert "C.E. $\times$ Farmer Training";
	lab var tr_iXcert "C.E. $\times$ Exporter Training";
	lab var tr_jXcert "C.E. $\times$ Joint Training";
	lab var sum_overmrl_eu "Residue $\geq$ E.U. Limit";
	lab var progmeet "Buyer was program participant";
	lab var dist_nearest_treated "Distance to nearest jointly trained farmer (km)";
	lab var farmer_in2 "Number of farmers within 2km distance";
	lab var trader_in3 "Number of exporters within 3km distance";
	lab var trader_in2 "Number of exporters within 2km distance";
	lab var dist_jointtrader "Distance to nearest jointly trained exporter (km)";	
	
		
/*-----------------------------------------------------------------------------------
			 		IMPACT ON TRADE WITH INTERMEDIARIES (Online Appendix Table A-16)
-----------------------------------------------------------------------------------*/	

** Setting;
	loc titles1 "& Within & Within & Outside & Within & Outside \\ ";
	loc titles2 "& cluster & cluster & cluster & cluster & cluster \\ \cmidrule{2-6}";
	loc numbers "& (1) & (2) & (3) & (4) & (5)  \\ \midrule";
	loc reg_table cells(b(fmt(3) star) se(par fmt(3))) starlevels(* 0.10 ** 0.05 *** 0.01) ///
		stats(betafi betafj betaij betafij ymean_control r2 N, fmt(2 2 2 2 2 2 0) ///
		labels("P-value ($ \text{H}_0: \beta_{\text{farmer}} = \beta_{\text{int}}$)" "P-value ($ \text{H}_0: \beta_{\text{farmer}} = \beta_{\text{joint}}$)" "P-value ($ \text{H}_0: \beta_{\text{int}} = \beta_{\text{joint}}$)" "P-value ($ \text{H}_0: \beta_{\text{farmer}} + \beta_{\text{int}} = \beta_{\text{joint}}$)" "Control mean" "R-squared" "Observations"))  ///
		label mlabels(none) collabels(none) nonotes nonumbers posthead("`titles1'" "`titles2'" "`numbers'") ///
		mgroups("Any Trade" "Spot Trade" "Contract Trade", pattern(1 1 0 1 0) ///
		span prefix(\multicolumn{@span}{c}{) suffix(}) erepeat(\cmidrule(lr){@span})) ///
		;

	local notes_ols_contractintermediary \item Notes: This table reports treatment effects on contract trading between farmers and intermediaries. ///
	The results use farm-gate sales data from two follow-up survey rounds. The dependent variable in Column 1 indicates whether the farmer traded with intermediary from same training group. Columns 2-3 report coefficient estimates on ispot trade during the survey period. Columns 4-5 report estimates on informal or formal contract trade. Within cluster refers to trade with exporters in the same training cluster and Outside cluster refers to any intermediary, exporter or collector, not in the same training cluster. ///
	Standard errors are clustered by farmer group and reported in parentheses. ///
	All specifications include farmer and intermediary characteristics at baseline as control variables as well as strata and survey round fixed effects. * denotes false discovery rate controlled statistical significance at 0.10, ** at 0.05, and *** at 0.01.;

** Estimation - First Survey;

** Estimation;
		eststo: qui reg progmeet tr_farm tr_int tr_joint certelig tr_fXcert tr_iXcert tr_jXcert `balance' `intbalance'  i.strata if round == 1, robust cluster(group_id);
			estadd ysumm;
		qui test tr_farm = tr_int;
		qui estadd scalar betafi = r(p);		
		qui test tr_farm = tr_joint;
		qui estadd scalar betafj = r(p);
		qui test tr_int = tr_joint;
		qui estadd scalar betaij = r(p);		
		qui test tr_farm+tr_int = tr_joint;
		qui estadd scalar betafij = r(p);	
			qui sum progmeet if dum_tgroup1 == 1;
			qui estadd scalar ymean_control = r(mean);
			estadd scalar pr2 = e(r2);	

		
		eststo: qui reg spot_within tr_farm tr_int tr_joint certelig tr_fXcert tr_iXcert tr_jXcert `balance' `intbalance' i.strata if round == 1, robust cluster(group_id);
			estadd ysumm;
		
		qui test tr_farm = tr_int;
		qui estadd scalar betafi = r(p);		
		qui test tr_farm = tr_joint;
		qui estadd scalar betafj = r(p);
		qui test tr_int = tr_joint;
		qui estadd scalar betaij = r(p);		
		qui test tr_farm+tr_int = tr_joint;
		qui estadd scalar betafij = r(p);	
			qui sum spot_within if dum_tgroup1 == 1 & round == 1;
			qui estadd scalar ymean_control = r(mean);
			estadd scalar pr2 = e(r2);


		eststo: qui reg spot_outside tr_farm tr_int tr_joint certelig tr_fXcert tr_iXcert tr_jXcert `balance' `intbalance' i.strata if round == 1, robust cluster(group_id);
			estadd ysumm;
		
		qui test tr_farm = tr_int;
		qui estadd scalar betafi = r(p);		
		qui test tr_farm = tr_joint;
		qui estadd scalar betafj = r(p);
		qui test tr_int = tr_joint;
		qui estadd scalar betaij = r(p);		
		qui test tr_farm+tr_int = tr_joint;
		qui estadd scalar betafij = r(p);	
			qui sum spot_outside if dum_tgroup1 == 1 & round == 1;
			qui estadd scalar ymean_control = r(mean);
			estadd scalar pr2 = e(r2);			

		eststo: qui reg contract_within tr_farm tr_int tr_joint certelig tr_fXcert tr_iXcert tr_jXcert `balance' `intbalance' i.strata if round == 1, robust cluster(group_id);
			estadd ysumm;
		
		qui test tr_farm = tr_int;
		qui estadd scalar betafi = r(p);		
		qui test tr_farm = tr_joint;
		qui estadd scalar betafj = r(p);
		qui test tr_int = tr_joint;
		qui estadd scalar betaij = r(p);		
		qui test tr_farm+tr_int = tr_joint;
		qui estadd scalar betafij = r(p);	
			qui sum contract_within if dum_tgroup1 == 1 & round == 1;
			qui estadd scalar ymean_control = r(mean);
			estadd scalar pr2 = e(r2);


		eststo: qui reg contract_outside tr_farm tr_int tr_joint certelig tr_fXcert tr_iXcert tr_jXcert `balance' `intbalance' i.strata if round == 1, robust cluster(group_id);
			estadd ysumm;
		
		qui test tr_farm = tr_int;
		qui estadd scalar betafi = r(p);		
		qui test tr_farm = tr_joint;
		qui estadd scalar betafj = r(p);
		qui test tr_int = tr_joint;
		qui estadd scalar betaij = r(p);		
		qui test tr_farm+tr_int = tr_joint;
		qui estadd scalar betafij = r(p);	
			qui sum contract_outside if dum_tgroup1 == 1 & round == 1;
			qui estadd scalar ymean_control = r(mean);
			estadd scalar pr2 = e(r2);		


** Table;
	loc colnum 5;
	local caption Impact of Training on Contract Trade - First Round;
	local label contractintermediary_result;
	local notes `notes_ols_contractintermediary';

head_foot, caption(`caption') label(`label') notes(`notes') columns(`colnum');

esttab  ///;
using "${table_path}/reg_contractintermediary_fu1.tex", replace ///
	keep(tr_farm tr_int tr_joint certelig tr_fXcert tr_iXcert tr_jXcert) `reg_table' ///
	prehead("$header") postfoot("$footer") substitute("\_" "_");

est clear;
		

** Estimation - Second Survey;
		
		eststo: qui reg progmeet tr_farm tr_int tr_joint certelig tr_fXcert tr_iXcert tr_jXcert `balance' `intbalance'  i.strata if round == 2, robust cluster(group_id);
			estadd ysumm;
		qui test tr_farm = tr_int;
		qui estadd scalar betafi = r(p);		
		qui test tr_farm = tr_joint;
		qui estadd scalar betafj = r(p);
		qui test tr_int = tr_joint;
		qui estadd scalar betaij = r(p);		
		qui test tr_farm+tr_int = tr_joint;
		qui estadd scalar betafij = r(p);	
			qui sum progmeet if dum_tgroup1 == 1 & round == 2;
			qui estadd scalar ymean_control = r(mean);
			estadd scalar pr2 = e(r2);	

		
		eststo: qui reg spot_within tr_farm tr_int tr_joint certelig tr_fXcert tr_iXcert tr_jXcert `balance' `intbalance' i.strata if round == 2, robust cluster(group_id);
			estadd ysumm;
		
		qui test tr_farm = tr_int;
		qui estadd scalar betafi = r(p);		
		qui test tr_farm = tr_joint;
		qui estadd scalar betafj = r(p);
		qui test tr_int = tr_joint;
		qui estadd scalar betaij = r(p);		
		qui test tr_farm+tr_int = tr_joint;
		qui estadd scalar betafij = r(p);	
			qui sum spot_within if dum_tgroup1 == 1 & round == 2;
			qui estadd scalar ymean_control = r(mean);
			estadd scalar pr2 = e(r2);


		eststo: qui reg spot_outside tr_farm tr_int tr_joint certelig tr_fXcert tr_iXcert tr_jXcert `balance' `intbalance' i.strata if round == 2, robust cluster(group_id);
			estadd ysumm;
		
		qui test tr_farm = tr_int;
		qui estadd scalar betafi = r(p);		
		qui test tr_farm = tr_joint;
		qui estadd scalar betafj = r(p);
		qui test tr_int = tr_joint;
		qui estadd scalar betaij = r(p);		
		qui test tr_farm+tr_int = tr_joint;
		qui estadd scalar betafij = r(p);	
			qui sum spot_outside if dum_tgroup1 == 1 & round == 2;
			qui estadd scalar ymean_control = r(mean);
			estadd scalar pr2 = e(r2);			

		eststo: qui reg contract_within tr_farm tr_int tr_joint certelig tr_fXcert tr_iXcert tr_jXcert `balance' `intbalance' i.strata if round == 2, robust cluster(group_id);
			estadd ysumm;
		
		qui test tr_farm = tr_int;
		qui estadd scalar betafi = r(p);		
		qui test tr_farm = tr_joint;
		qui estadd scalar betafj = r(p);
		qui test tr_int = tr_joint;
		qui estadd scalar betaij = r(p);		
		qui test tr_farm+tr_int = tr_joint;
		qui estadd scalar betafij = r(p);	
			qui sum contract_within if dum_tgroup1 == 1 & round == 2;
			qui estadd scalar ymean_control = r(mean);
			estadd scalar pr2 = e(r2);


		eststo: qui reg contract_outside tr_farm tr_int tr_joint certelig tr_fXcert tr_iXcert tr_jXcert `balance' `intbalance' i.strata if round == 2, robust cluster(group_id);
			estadd ysumm;
		
		qui test tr_farm = tr_int;
		qui estadd scalar betafi = r(p);		
		qui test tr_farm = tr_joint;
		qui estadd scalar betafj = r(p);
		qui test tr_int = tr_joint;
		qui estadd scalar betaij = r(p);		
		qui test tr_farm+tr_int = tr_joint;
		qui estadd scalar betafij = r(p);	
			qui sum contract_outside if dum_tgroup1 == 1 & round == 2;
			qui estadd scalar ymean_control = r(mean);
			estadd scalar pr2 = e(r2);	


** Table;
	loc colnum 5;
	local caption Impact of Training on Contract Trade - Second Survey;
	local label contractintermediary_result;
	local notes `notes_ols_contractintermediary';

head_foot, caption(`caption') label(`label') notes(`notes') columns(`colnum');

esttab  ///;
using "${table_path}/reg_contractintermediary_fu2.tex", replace ///
	keep(tr_farm tr_int tr_joint certelig tr_fXcert tr_iXcert tr_jXcert) `reg_table' ///
	prehead("$header") postfoot("$footer") substitute("\_" "_");

est clear;


/*-----------------------------------------------------------------------------------
		Farmer's trust on intermediary and Contract Trade (Online Appendix Table A-17)
-----------------------------------------------------------------------------------*/	

gen newid = farmerid if farmerid != .;

merge m:1 newid using `game';
keep if _merge == 3;
drop _merge;
gen g1_partner_int = (g1_partnernewid >= 20000 & g1_partnernewid !=.);
gen g2_partner_int = (g2_partnernewid >= 20000 & g2_partnernewid !=.);
gen g3_partner_int = (g3_partnernewid >= 20000 & g3_partnernewid !=.);
gen g1_partnershare = g1_partner/100;
gen g2_partnershare = g2_partner/100;
gen g3_partnershare = g3_partner/150;

lab var g1_partnershare "Proportion passed to exporter in dictator game";
lab var g2_partnershare "Proportion passed to exporter in trust game";
lab var g3_partnershare "Passed to exporter in 2nd stage of trust game";

xi, prefix(C) noomit i.buyer_contract*i.progmeet;

	loc titles1 "& \multicolumn{2}{c}{Any trade} & \multicolumn{2}{c}{Contract trade} & \multicolumn{2}{c}{Spot trade} \\ \cmidrule(lr){2-3} \cmidrule(lr){4-5} \cmidrule(lr){6-7} ";
	*loc titles2 "& cluster & cluster & cluster & cluster & cluster \\ \cmidrule{2-6}";
	loc numbers "& (1) & (2) & (3) & (4) & (5) & (6)  \\ \midrule";
	loc reg_table cells(b(fmt(3) star) se(par fmt(3))) starlevels(* 0.10 ** 0.05 *** 0.01) ///
		stats(ymean_control r2 N, fmt(2 2 0) ///
		labels("Control mean" "R-squared" "Observations"))  ///
		label mlabels(none) collabels(none) nonotes nonumbers posthead("`titles1'" "`numbers'") ///
		mgroups("Trade with intermediary in same cluster", pattern(1 0 0 0 0 0) ///
		span prefix(\multicolumn{@span}{c}{) suffix(}) erepeat(\cmidrule(lr){@span})) ///
		;

	local notes_trust_contracttrade \item Notes: This table reports results based on sample of farmers matched with intermediary ///
	partners in dictator and trust games. Specifications include farmer characteristics at baseline as control variables and strata and round fixed effects. ///
	Standard errors are clustered by farmer-intermediary cluster and reported in parentheses. * denotes statistical significance at 0.10, ** at 0.05, and *** at 0.01.; 	
		

	eststo: qui reg progmeet g2_partnershare i.round i.strata `balance' if g2_partner_int == 1, robust cluster(group_id);
	estadd ysumm;
	qui sum progmeet if dum_tgroup1 == 1;
	qui estadd scalar ymean_control = r(mean);		

	eststo: qui reg progmeet g1_partnershare i.round i.strata `balance' if g1_partner_int == 1, robust cluster(group_id);
	estadd ysumm;
	qui sum progmeet if dum_tgroup1 == 1;
	qui estadd scalar ymean_control = r(mean);		

	eststo: qui reg CbuyXpro_2_1 g2_partnershare i.round i.strata `balance' if g2_partner_int == 1, robust cluster(group_id);
	estadd ysumm;
	qui sum CbuyXpro_2_1 if dum_tgroup1 == 1;
	qui estadd scalar ymean_control = r(mean);	

	eststo: qui reg CbuyXpro_2_1 g1_partnershare i.round i.strata `balance' if g1_partner_int == 1, robust cluster(group_id);
	estadd ysumm;
	qui sum CbuyXpro_2_1 if dum_tgroup1 == 1;
	qui estadd scalar ymean_control = r(mean);

	eststo: qui reg CbuyXpro_3_1 g2_partnershare i.round i.strata `balance' if g2_partner_int == 1, robust cluster(group_id);
	estadd ysumm;
	qui sum CbuyXpro_3_1 if dum_tgroup1 == 1;
	qui estadd scalar ymean_control = r(mean);	

	eststo: qui reg CbuyXpro_3_1 g1_partnershare i.round i.strata `balance' if g1_partner_int == 1, robust cluster(group_id);
	estadd ysumm;
	qui sum CbuyXpro_3_1 if dum_tgroup1 == 1;
	qui estadd scalar ymean_control = r(mean);
	

** Table;
	loc colnum 6;
	local caption Farmer's Trust on Intermediary and Contract Trade;
	local label trust_contracttrade;
	local notes `notes_trust_contracttrade';

head_foot, caption(`caption') label(`label') notes(`notes') columns(`colnum');

esttab  ///;
using "${table_path}/reg_trust_contracttrade.tex", replace ///
	keep(g1_partnershare g2_partnershare) `reg_table' ///
	prehead("$header") postfoot("$footer") substitute("\_" "_");

est clear;	
clear;

/*-----------------------------------------------------------------------------------
	CORRELATION BETWEEN PREVIOUS VIETGAP CERTIFICATE AND FARMER CHARACTERISTICS (Online Appendix Table A-18)
-----------------------------------------------------------------------------------*/
use `maindata';
gen vietgap_2yr = inlist(vietgap_year, 2017, 2018);
gen cost_total_base = cost_fert_base + cost_pest_base + cost_facility_base + cost_equip_base + cost_labor_base + cost_utility_base;
gen log_cost_total_base = log(cost_total_base);

	lab var age "Age";
	lab var female "Female";
	lab var edu_second "Secondary Education";
	lab var exp_dragon "Experience growing dragon fruit (years)";
	lab var ha_whitedragon "Size of dragon fruit farm (hectares)";
	lab var tree_whitedragon "Number of dragon fruit trees";
	lab var anycert_base "Received any agricultural certificate before";
	lab var loan_any_base "Ever received loan for farm investment";
	lab var save_bank_base "Ever saved at bank";
	lab var mean_trust_base "Mean score on trust measurement";
	lab var mean_business_base "Mean score on business measurement";
	lab var mean_confidence_base "Mean score on confidence measurement";
	lab var raven_total "Raven matrices score";
	lab var risky_invest "Risk investment";
	lab var present_bias_base "Time discounting - present biased";
	lab var gap_comply_base "Self-reported GAP compliance";
	lab var hours_base "Farm work hours";
	lab var log_cost_total_base "Total Farm Input Expenditure";
	lab var market_china_base "Export to China";
	lab var market_asia_base "Export to Asia";
	lab var market_eu_base "Export to EU";


** Setting;
	loc titles "& \multicolumn{2}{c}{OLS} & \multicolumn{2}{c}{Logit}  \\ \cmidrule(lr){2-3} \cmidrule(lr){4-5}";
	loc numbers "& (1) & (2) & (3) & (4) \\ \midrule";
	loc reg_table cells(b(fmt(3) star) se(par fmt(3))) starlevels(* 0.10 ** 0.05 *** 0.01) ///
		stats(stratafe ymean_control r2 N, fmt(0 2 2 0) ///
		labels("Strata FE" "Mean" "R-squared" "Observations"))  ///
		label mlabels(none) collabels(none) nonotes nonumbers posthead("`titles'" "`numbers'") ///
		mgroups("Received VietGAP certificate within 2 years", pattern(1 0 0 0) ///
		span prefix(\multicolumn{@span}{c}{) suffix(}) erepeat(\cmidrule(lr){@span})) 
		;

		
keep if round == 1;	
		
** Estimation;		
		local outcome "vietgap_2yr";


		foreach y of local outcome {;
		eststo: qui reg `y' age female edu_second exp_dragon ha_whitedragon loan_any_base save_bank_base mean_trust_base mean_business_base mean_confidence_base risky_invest raven_total present_bias_base if round == 1, robust cluster(group_id)	;
		estadd ysumm;
		qui estadd local stratafe "No", replace;
		qui sum `y';
		qui estadd scalar ymean_control = r(mean);
		
		eststo: qui reg `y' age female edu_second exp_dragon ha_whitedragon loan_any_base save_bank_base mean_trust_base mean_business_base mean_confidence_base risky_invest raven_total present_bias_base i.strata if round == 1, robust cluster(group_id);	
		estadd ysumm;
		qui estadd local stratafe "Yes", replace;
		qui sum `y';
		qui estadd scalar ymean_control = r(mean);		
		
		qui logit `y' age female edu_second exp_dragon ha_whitedragon loan_any_base save_bank_base mean_trust_base mean_business_base mean_confidence_base risky_invest raven_total present_bias_base if round == 1, robust cluster(group_id);
		local r2p = e(r2_p)	;	
		
		eststo: qui margins, dydx(age female edu_second exp_dragon ha_whitedragon loan_any_base save_bank_base mean_trust_base mean_business_base mean_confidence_base risky_invest raven_total present_bias_base) atmeans post;
		estadd ysumm;
		qui estadd local stratafe "No", replace	;
		qui sum `y';
		qui estadd scalar ymean_control = r(mean);
		estadd scalar r2 = `r2p';
		
		qui logit `y' age female edu_second exp_dragon ha_whitedragon loan_any_base save_bank_base mean_trust_base mean_business_base mean_confidence_base risky_invest raven_total present_bias_base i.strata if round == 1, robust cluster(group_id);
		local r2p = e(r2_p);
		
		eststo: qui margins, dydx(age female edu_second exp_dragon ha_whitedragon loan_any_base save_bank_base mean_trust_base mean_business_base mean_confidence_base risky_invest raven_total present_bias_base i.strata) atmeans post;
		estadd ysumm;
		qui estadd local stratafe "Yes", replace;	
		qui sum `y';
		qui estadd scalar ymean_control = r(mean);			
		estadd scalar r2 = `r2p';	
		

** Table;
	loc colnum 4;
	local caption Baseline Farmer Characteristics and VietGAP Certificate;
	local label vietgap_price;
	local notes `notes_sales';

head_foot, caption(`caption') label(`label') notes(`notes') columns(`colnum');

esttab  ///
using "${table_path}/reg_farmerchar_`y'.tex", replace ///
	keep(age female edu_second exp_dragon ha_whitedragon loan_any_base save_bank_base mean_trust_base mean_business_base mean_confidence_base risky_invest raven_total present_bias_base) `reg_table' ///
	prehead("$header") postfoot("$footer") substitute("\_" "_");
est clear;
		};
		
clear;		
	
/*-----------------------------------------------------------------------------------
			RELATIONSHIP BETWEEN PREVIOUS VIETGAP CERTIFICATE AND PRICE (Online Appendix Table A-19)
-----------------------------------------------------------------------------------*/
use `maindata';

** Setting;
	loc titles "& \multicolumn{4}{c}{Log(Price received at farm-gate)}  \\ \cmidrule{2-5}";
	loc numbers "& (1) & (2) & (3) & (4)  \\ \midrule";
	loc reg_table cells(b(fmt(3) star) se(par fmt(3))) starlevels(* 0.10 ** 0.05 *** 0.01) ///
		stats(ymean_control r2 N, fmt(2 2 0) ///
		labels("Mean of Dep Var (1000 VND per kg)" "R-squared" "Observations"))  ///
		label mlabels(none) collabels(none) nonotes nonumbers posthead("`titles'" "`numbers'");


	local notes_pricepremium \item Notes: This table reports estimates from an ordinary least squares regression of price on product characteristics. ///
	The results use farm-gate sales data from two follow-up survey rounds. Farm-gate price is the price farmer received in each sales transaction. /// 
	Standard errors are clustered by farmer group and reported in parentheses. China market is omitted in specification. GAP compliance is the standardized score on the GAP audit. ///
	All product characteristics are standardized by the control group's mean and standard deviation. All specifications include farmer characteristics (age, female, education, experience, size of farm, time discounting, ///
	raven matrices score, savings at bank, loans, and measures on trust, entrepreneurship, and confidence) at baseline as well as strata and survey round fixed effects. * denotes statistical significance at 0.10, ** at 0.05, and *** at 0.01.; 		
		

local balance2 age female edu_second exp_dragon ha_whitedragon white_age_less1 white_age_less3 white_age_less10 white_age_less20 white_age_more20 loan_any_base save_bank_base mean_trust_base mean_business_base mean_confidence_base raven_total present_bias_base totalvolume_base;

lab var vietgap "Received VietGAP certificate before";
lab var std_score "Standardized GAP audit score";
lab var std_meanquality "Product attribute index (mean of z-score)";

gen vietgap_2yr = inlist(vietgap_year, 2017, 2018);
lab var vietgap_2yr "Have VietGAP certificate at baseline";


preserve;

** Estimation;
	local outcome "price";
	local vietgap "vietgap_2yr";

		foreach y of local outcome {;
		eststo: qui reg log_`y' `vietgap' `balance2' `intbalance' i.strata i.round, robust cluster(group_id);
		estadd ysumm;
		qui sum `y';
		qui estadd scalar ymean_control = r(mean);

		eststo: qui reg log_`y' `vietgap' std_score `balance2' `intbalance' i.strata i.round, robust cluster(group_id);	
		estadd ysumm;
		qui sum `y';
		qui estadd scalar ymean_control = r(mean);
		
		eststo: qui reg log_`y' `vietgap' std_meanquality `balance2' `intbalance' i.strata i.round, robust cluster(group_id);	
		estadd ysumm;
		qui sum `y';
		qui estadd scalar ymean_control = r(mean);	

		eststo: qui reg log_`y' `vietgap' std_score std_meanquality `balance2' `intbalance' i.strata i.round, robust cluster(group_id);
		estadd ysumm;
		qui sum `y';
		qui estadd scalar ymean_control = r(mean);		
		
		
		};

** Table;
	loc colnum 5;
	local caption Price Premium on VietGAP Certificate and Product Quality;
	local label vietgap_price;
	local notes `notes_pricepremium';

head_foot, caption(`caption') label(`label') notes(`notes') columns(`colnum');

esttab  ///
using "${table_path}/reg_vietgap_price_pool.tex", replace ///
	keep(`vietgap' std_score std_meanquality) `reg_table' ///
	prehead("$header") postfoot("$footer") substitute("\_" "_");
est clear;
restore;

























	
	
	
	
	
	
	
	
	
	
	
	
	
	

	
